package cn.wuxun.news.service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import cn.wuxun.common.pojo.News;
import cn.wuxun.common.vo.NewsUIResult;
import cn.wuxun.common.vo.PageResult;
import cn.wuxun.news.mapper.NewsMapper;

@Service
public class NewsService {
	@Autowired
	NewsMapper newsMapper;
	
	public List<News> queryNewsTitle(String ntid) {
		return newsMapper.queryNewsTitle(ntid);
	}

	public PageResult queryNewsPage(Integer currentPage, Integer pageSize) {
		// 封装数据到NewsUIResult对象
		// 1准备一个返回的对象,将查询数据set进去然后返回
		PageResult result = new PageResult();
		Integer totalPage;

		// 2封装第一个属性 total 的数量
		Integer total = newsMapper.queryTotal();

		// 3封装第二个属性List<Product> nList
		// 根据页数计算起始位置
		Integer start = (currentPage - 1) * pageSize;
		
		List<News> nList = newsMapper.queryByPage(start, pageSize);
		
		if ((total % pageSize) == 0) {
			totalPage = total / pageSize;
		} else {
			totalPage = total / pageSize + 1;
		}
		// 4封装对象属性
		result.setCurrentPage(currentPage);
		result.setList(nList);
		result.setTotal(total);
		result.setTotalPage(totalPage);
		return result;
	}

	public News queryNewsByNid(String nid) {
		return newsMapper.queryNews(nid);
	}
	
	
	public void addNews(News news) {
		// 补充数据 nid ntime
		news.setNid(UUID.randomUUID().toString());
		news.setNtime(new Date());
		newsMapper.addNews(news);
	}

	public void delNewsById(String nid) {
		newsMapper.delNews(nid);
	}

	public void updateNews(News news) {
		//补充ntime
		news.setNtime(new Date());
		newsMapper.updateNews(news);
	}

	public List<News> queryNewsByNtidAndHot(String ntid) {
		// TODO Auto-generated method stub
		return newsMapper.queryNewsByNtidAndHot(ntid);
	}

	public List<News> queryNewsByNtid(String ntid) {
		// TODO Auto-generated method stub
		return newsMapper.queryNewsByNtid(ntid);
	}

}
